﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.GoodsDelivery;
using MINERP.COM;

namespace ERPGoodsDelivery.Return
{
    public partial class FrmReturnBase : Form
    {
        //属性
        private static FrmReturnBase _instance;
        public static FrmReturnBase GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmReturnBase();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        //参数
        private GoodsReturnBLL bll = new GoodsReturnBLL();
        private BaseInfoBLL baseBll = new BaseInfoBLL();
        private GoodsDeliveryBLL userBll = new GoodsDeliveryBLL();

        //bool
        private bool SaveModel = false;                                     //false 添加模式;  true 修改模式

        public FrmReturnBase()
        {
            InitializeComponent();
        }

        private void FrmReturnInfo_Load(object sender, EventArgs e)
        {
            BindReturnPackageNotFectch();
            BindAreas();
            BindStation();
            BindStationGoods();
            BindUser();
        }

        //取件
        private void tsGet_Click(object sender, EventArgs e)
        {
            try
            {
                FrmGetReturnPackage frm = FrmGetReturnPackage.GetInstance;
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void tsDispose_Click(object sender, EventArgs e)
        {
            try
            {
                FrmDispose frm = FrmDispose.GetInstance;
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void tsFind_Click(object sender, EventArgs e)
        {
            try
            {
                Return.FrmReturnFind frm = Return.FrmReturnFind.GetInstance;
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //按不同模式操作
        private void tscbModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (tscbModel.Text == "修改模式")
                {
                    SaveModel = true; 
                    SetReturnPackage(gvReturnPackage.GetSelectedRows()[0]);
                }
                else
                {
                    SaveModel = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //单击
        private void gvReturnPackage_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                if (SaveModel == true)                  //修改模式
                {
                    SetReturnPackage(e.RowHandle);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //焦点发生改变
        private void gvReturnPackage_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (e.FocusedRowHandle >= 0)
                {
                    if (SaveModel == true)                  //修改模式
                    {
                        SetReturnPackage(e.FocusedRowHandle);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //保存(修改)退件包
        private void tsSaveReturn_Click(object sender, EventArgs e)
        {
            try
            {
                if (SaveModel == false)             //新增
                {
                    InsertReturnPackage();
                    BindReturnPackageNotFectch();
                }
                else if (SaveModel == true)         //修改
                {
                    int returnId = Convert.ToInt32(gvReturnPackage.GetDataRow(gvReturnPackage.GetSelectedRows()[0])["ReturnId"]);
                    UpdateReturnPackage(returnId);
                    BindReturnPackageNotFectch();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //删除退件包
        private void tsDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (gvReturnPackage.SelectedRowsCount == 0)
                {
                    MessageBox.Show("无退件包存在！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("确定要删除该退件包吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    int returnId = Convert.ToInt32(gvReturnPackage.GetRowCellValue(gvReturnPackage.GetSelectedRows()[0], "ReturnId"));
                    DelReturnPackage(returnId);
                    BindReturnPackageNotFectch();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //刷新
        private void tsRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                BindReturnPackageNotFectch();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //--------------公共方法------------------
        /// <summary>
        /// 绑定地区
        /// </summary>
        private void BindAreas()
        {
            try
            {
                List<string> areas = bll.GetAreas();
                cbArea.DataSource = areas;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 绑定客运站
        /// <summary>
        /// 绑定客运站
        /// </summary>
        private void BindStation()
        {
            try
            {
                List<BaseInfoBLL.StationInfo> stations = baseBll.GetStationInfo();
                cbDeliverObject.DisplayMember = "StationName";
                cbDeliverObject.ValueMember = "StationId";
                cbDeliverObject.DataSource = stations;
                cbDeliverObject.Text = "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 绑定货运站及其电话
        /// <summary>
        /// 绑定货运站及其电话
        /// </summary>
        private void BindStationGoods()
        {
            try
            {
                List<BaseInfoBLL.StationGoodsInfo> stationGoods = baseBll.GetStationGoodsInfo();
                cbStationRemark.DisplayMember = "StationGoodsNamePhone";
                cbStationRemark.ValueMember = "StationGoodsId";
                cbStationRemark.DataSource = stationGoods;
                cbStationRemark.Text = "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 绑定录入人员
        /// <summary>
        /// 绑定录入人员
        /// </summary>
        private void BindUser()
        {
            try
            {
                DataTable dt = userBll.GetDeliveryWorkers();
                cbUser.DisplayMember = "UserName";
                cbUser.ValueMember = "UserId";
                cbUser.DataSource = dt;
                cbUser.Text = "";
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 绑定未取件的退件包
        /// <summary>
        /// 绑定未取件的退件包
        /// </summary>
        private void BindReturnPackageNotFectch()
        {
            try
            {
                int notFetch=(int)ReturnPackageStatus.NotFetch;
                DataTable dt = bll.GetReturnPackage(notFetch);
                gcReturnPackage.DataSource = dt;
                gcReturnPackage.RefreshDataSource();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 新增退件包
        /// <summary>
        /// 新增退件包
        /// </summary>
        private void InsertReturnPackage()
        {
            try
            {
                string area = cbArea.Text;                                     //地区
                int stationId = Convert.ToInt32(cbDeliverObject.SelectedValue);         //客运站Id
                int stationGoodsId = Convert.ToInt32(cbStationRemark.SelectedValue);    //货运站及电话Id
                int userId = Convert.ToInt32(cbUser.SelectedValue);                     //录入人员
                int notFetch = (int)ReturnPackageStatus.NotFetch;            //退件包状态未为未取件
                bll.InsertReturnPackage(area, stationId, stationGoodsId, userId, notFetch);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改退件包
        /// <summary>
        /// 修改退件包
        /// </summary>
        /// <param name="returnId"></param>
        private void UpdateReturnPackage(int returnId)
        {
            string area = cbArea.Text;                                     //地区
            int stationId = Convert.ToInt32(cbDeliverObject.SelectedValue);         //客运站Id
            int stationGoodsId = Convert.ToInt32(cbStationRemark.SelectedValue);    //货运站及电话Id
            int userId = Convert.ToInt32(cbUser.SelectedValue);                     //录入人员
            bll.UpdateReturnPackage(area, stationId, stationGoodsId, userId, returnId);
        }
        #endregion

        #region 设置退件包
        /// <summary>
        /// 设置退件包
        /// </summary>
        /// <param name="returnId"></param>
        private void SetReturnPackage(int returnId)
        {
            try
            {
                DataRow row = gvReturnPackage.GetDataRow(returnId);
                cbArea.Text = row["Area"].ToString();
                cbDeliverObject.Text = row["StationName"].ToString();
                cbStationRemark.Text = row["StationGoodsNamePhone"].ToString();
                cbUser.Text = row["UserName"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 删除退件包
        /// <summary>
        /// 删除退件包
        /// </summary>
        /// <param name="returnId"></param>
        private void DelReturnPackage(int returnId)
        {
            try
            {
                bll.DelReturnPackage(returnId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}
